#include "localfolderdialog.h"
#include "ui_localfolderdialog.h"
#include <QGraphicsDropShadowEffect>
#include <QMouseEvent>
#include <QDebug>
#include <QFileDialog>
#include <QScrollArea>
#include <QCheckBox>
LocalFolderDialog::LocalFolderDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::LocalFolderDialog)
{
    ui->setupUi(this);

    // 设置阴影:
    setWindowFlag(Qt::FramelessWindowHint);
    setAttribute(Qt::WA_TranslucentBackground);
    QGraphicsDropShadowEffect* effect = new QGraphicsDropShadowEffect(this);
    effect->setColor(QColor(0, 0, 0, 66));
    effect->setOffset(0, 0);
    effect->setBlurRadius(20);
    setGraphicsEffect(effect);

    // 初始化body
    QLayout *mainLayout = ui->body->layout();
    mainLayout->setContentsMargins(0, 0, 9, 0);
    QScrollArea *scrollArea = ui->scrollArea;
    QLayout *containerLayout = ui->container->layout();
    containerLayout->setSpacing(6);                   // 控件间固定 6px 间距
    containerLayout->setContentsMargins(0, 0, 0, 0);
    containerLayout->setAlignment(Qt::AlignTop);       // 布局内所有子控件顶对齐
    scrollArea->setWidgetResizable(true);
}

LocalFolderDialog::~LocalFolderDialog()
{
    delete ui;
}

void LocalFolderDialog::mouseMoveEvent(QMouseEvent *event)
{
        if(event->buttons() == Qt::LeftButton){
            if(!dx && !dy){
                dx = event->x();
                dy = event->y();
            }
            if(dy > ui->head->height() || dx > ui->head->width());
            else // 只能通过头部区域拖动窗口
            {
                int x = event->globalX() - dx;
                int y = event->globalY() - dy;
                move(x, y);
                return;
            }
        }
        QWidget::mouseMoveEvent(event);
}

void LocalFolderDialog::mouseReleaseEvent(QMouseEvent *event)
{
        if(dx || dy) {
            dx = dy = 0;
            return;
        }
        QWidget::mouseReleaseEvent(event);
}

void LocalFolderDialog::initPaths(const QStringList &paths)
{
    _paths = paths;
    for(const auto& path : _paths){
        qDebug() << path;
        QLayout *containerLayout = ui->container->layout();
        auto btn = new QCheckBox(path, ui->body);
        btn->setMinimumSize(QSize(500, 55));
        btn->setMaximumSize(QSize(500, 55));
        btn->setStyleSheet ("QCheckBox {font-size:20px;}"
                            "QCheckBox::indicator { width:20px; height:20px;}"
                            "QCheckBox::indicator:checked { background-color: red; border: 2px solid red; border-radius: 5px;} "
                            "QCheckBox::indicator:unchecked { border: 2px solid #787F8D; border-radius: 5px;}"
                            "QCheckBox::indicator:unchecked:hover { border: 2px solid #000000; border-radius: 5px;}");
        btn->setChecked(true);
        containerLayout->addWidget(btn);
    }
}

QStringList LocalFolderDialog::getPaths() const
{
    QStringList ret;
    QList<QCheckBox*> list = findChildren<QCheckBox*>();
    for(auto btn : list){
        if (btn->isChecked()){
            ret += btn->text();
        }
    }
    return ret;
}


void LocalFolderDialog::on_pushButton_close_clicked()
{
    qDebug() << "LocalFolderDialog pushButton_close_clicked";
    LocalFolderDialog::reject();
}

void LocalFolderDialog::on_pushButton_accept_clicked()
{
    qDebug() << "LocalFolderDialog on_pushButton_accept_clicked";
    LocalFolderDialog::accept();
}

void LocalFolderDialog::on_pushButton_selectFolder_clicked()
{
    QString path = QFileDialog::getExistingDirectory(this, "", ".");
    qDebug() << "选择了文件夹" << path;
    if (path.size()){
       if (_paths.contains(path)){
           qDebug() << "选过了";
       } else {
           _paths.push_back(path);
           QLayout *containerLayout = ui->container->layout();
           auto btn = new QCheckBox(path, ui->body);
           btn->setMinimumSize(QSize(500, 55));
           btn->setMaximumSize(QSize(500, 55));
           btn->setStyleSheet ("QCheckBox {font-size:20px;}"
                               "QCheckBox::indicator { width:20px; height:20px;}"
                               "QCheckBox::indicator:checked { background-color: red; border: 2px solid red; border-radius: 5px;} "
                               "QCheckBox::indicator:unchecked { border: 2px solid #787F8D; border-radius: 5px;}"
                               "QCheckBox::indicator:unchecked:hover { border: 2px solid #000000; border-radius: 5px;}");
           btn->setChecked(true);
           containerLayout->addWidget(btn);
       }
    }
}
